<? if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Item_model extends CI_Model
{
	public $num_edge_entries = 2;
	public $items_per_page = 15;

	public function add_new_item($data)
	{
		$this->db->insert('items', $data);
	}

	public function get_items()
	{
		//$this->db->order_by()
		return $this->db->get('items');
	}

	public function get_items_with_idx($idx)
	{
		$offset = $idx * $this->items_per_page;

		return $this->db
			->order_by('item_id', 'asc')
			->get('items', $this->items_per_page, $offset);
	}

	public function get_item_by_id($id=null)
	{
		if($id)
		{
			return $this->db->where('item_id', $id)->get('items')->row();
		}
		else
		{
			return (object) array(
				'item_id' => '',
				'item_name' => '',
				'item_unit' => '',
				'item_whole_sale_price' => '',
				'item_retail_price' => '',
				'item_quantity' => '');
		}
	}

	public function update_item($data, $id)
	{
		$this->db->where('item_id', $id)->update('items',$data);
	}

	public function delete_item_by_id($id)
	{
		$this->db->where('item_id', $id)->delete('items');
	}

	public function search_by_item_name($term)
	{
		return $this->db
			->like('item_name', $term)
			->limit(100)
			->get('items');
	}

	public function search_items_by_id($id)
	{
		return $this->db->where('item_id', $id)->get('items');
	}
}